﻿using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;

using IssueTrackerTest.IssueTrackerService;

namespace IssueTrackerTest
{
    /// <summary>
    /// Summary description for ProjectMemberTest
    /// </summary>
    [TestClass]
    public class ProjectMemberTest
    {
        [TestMethod]
        public void TestAddProjectMember()
        {
            IssueTrackerService.IssueTrackerServiceClient client
                = new IssueTrackerService.IssueTrackerServiceClient();
            
            IssueTrackerService.ProjectMember entity = new IssueTrackerService.ProjectMember();


            try
            {
                client.Open();
                List<IssueTrackerService.Project> projectList = client.ListProjects().ToList();

                if (projectList != null)
                {
                    IssueTrackerService.Project project = projectList.First();

                    entity.ProjectId = project.Id;
                    entity.UserId = "testuser";
                    entity.CreatorId = "testuser";
                    entity.UserId = "testuser";

                    IssueTrackerService.ProjectMember result = client.CreateProjectMember(entity);

                    Assert.IsNotNull(result.Id);
                }
                else
                {
                    Assert.Fail("Project Table is empty");
                }
            }
            catch (Exception ex)
            {
                Assert.Fail(ex.Message);
            }
            finally
            {
                client.Close();
            }
        }

        [TestMethod]
        public void TestListByRef()
        {
            IssueTrackerService.IssueTrackerServiceClient client
                = new IssueTrackerService.IssueTrackerServiceClient();

            client.Open();

            List<IssueTrackerService.Project> projectList;

            try
            {

                projectList = client.ListProjects().ToList();

                if (projectList != null)
                {
                    List<ProjectMember> childList = client.ListProjectMembersByRef(projectList.First().Id).ToList();

                    Assert.IsNotNull(childList);

                    Assert.IsTrue(childList.Count > 0);
                }

            }
            catch (Exception ex)
            {
                Assert.Fail(ex.Message);
            }
            finally
            {
                client.Close();
            }

        }

        [TestMethod]
        public void TestUpdateProjectMember()
        {
            IssueTrackerService.IssueTrackerServiceClient client
                = new IssueTrackerService.IssueTrackerServiceClient();

            List<IssueTrackerService.ProjectMember> list;

            try
            {
                client.Open();

                list = client.ListProjectMembers().ToList();

                if (list != null)
                {
                    IssueTrackerService.ProjectMember entity = list.First();

                    entity.UserId = "newuser";

                    IssueTrackerService.ProjectMember result = client.UpdateProjectMember(entity);

                    Assert.AreEqual("newuser", result.UserId);
                }
            }
            catch (Exception ex)
            {
                Assert.Fail(ex.Message);
            }
            finally
            {
                client.Close();
            }
            
            
        }

        [TestMethod]
        public void TestDeleteProjectMember()
        {
            IssueTrackerService.IssueTrackerServiceClient client
                = new IssueTrackerService.IssueTrackerServiceClient();

            int id;
            List<IssueTrackerService.ProjectMember> list;

            try
            {
                client.Open();
                list = client.ListProjectMembers().ToList();

                if (list != null)
                {
                    IssueTrackerService.ProjectMember entity = list.First();

                    id = entity.Id;

                    client.DeleteProjectMember(entity);

                    var checkProjectMember = client.GetProjectMember(id);

                    Assert.IsNull(checkProjectMember);
                }
                else
                {
                    Assert.Fail("ProjectMember Table is empty");
                }
            }
            catch (Exception ex)
            {
                Assert.Fail(ex.Message);
            }
            finally
            {
                client.Close();
            }
        }

   
    }
}
